package com.game;

import java.util.ArrayList;
import java.util.Random;

import android.util.Log;

public class PuzzleTemplatePath {
	
	private final static String TAG = PuzzleTemplatePath.class.getSimpleName(); 
	private Random random = null;
	
	private float width = 0;
	private float height = 0;
	
	private int row = 1;
	private int coloum = 1;
	
	private float startX = 0;
	private float startY = 0;
	
	private ArrayList<PuzzleUtil> utilList = null;
	
	PuzzleTemplatePath() {
		random = new Random();
		width = 0;
		height = 0;
		row = 1;
		coloum = 1;
		
		startX = 0;
		startY = 0;
	}
	
	PuzzleTemplatePath(int width, int height, int row, int coloum, float startX, float startY) {
		random = new Random();
		try {
			if (width < 0) {
				throw new Exception("Wrong width to PuzzlePath");
			}
			else {
				this.width = width;
			}
			if (height < 0) {
				throw new Exception("Wrong height to PuzzlePath");
			}
			else {
				this.height = height;
			}
			
			if (row < 1) {
				throw new Exception("Wrong row to PuzzleCreate");
			}
			else {
				this.row = row;
			}
			if (coloum < 1) {
				throw new Exception("Wrong coloum to PuzzleCreate");
			}
			else {
				this.coloum = coloum;
			}
			
			this.startX = startX;
			this.startY = startY;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setCL(int row, int coloum) {
		if (row > 0) {
			this.row = row;
		}
		if (coloum > 0) {
			this.coloum = coloum;
		}
	}
	
	public void setWH(float width, float height) {
		if (width > 0) {
			this.width = width;
		}
		if (height > 0) {
			this.height = height;
		}
	}
	
	public void setStartPos(float x, float y) {
		this.startX = x;
		this.startY = y;
	}
	
	public void setRandomUtil() {
		
		int curRow = 0;
		int curColoum = 0;
		float perWidth = width / coloum;
		float perHeight = height / row;
		
		int leftState = 0;
		int topState = 0;
		int rightState = 0;
		int bottomState = 0;
		
		
		utilList = new ArrayList<PuzzleUtil>();
		for (curRow = 0; curRow < row; curRow++) {
			for (curColoum = 0; curColoum < coloum; curColoum++) {
				PuzzleUtil _util = new PuzzleUtil();
				//随机初始化四边
				leftState = random.nextInt(2);
				leftState = (leftState == 1) ? leftState : (leftState - 1);
				topState = random.nextInt(2);
				topState = (topState == 1) ? topState : (topState - 1);
				rightState = random.nextInt(2);
				rightState = (rightState == 1) ? rightState : (rightState - 1);
				bottomState = random.nextInt(2);
				bottomState = (bottomState == 1) ? bottomState : (bottomState - 1);
				
				
				//设置上下边
				if (curRow == 0) {
					topState = PuzzleUtil.EDGE_FLAT;
				}
				else if (curRow > 0) {
					topState = PuzzleUtil.EDGE_FLAT - utilList.get((curRow - 1) * coloum + curColoum).getBottomState();
				}
				if (curRow == row - 1) {
					bottomState = PuzzleUtil.EDGE_FLAT;
				}
				//设置左右边
				if (curColoum == 0) {
					leftState = PuzzleUtil.EDGE_FLAT;
				}
				else if (curColoum > 0) {
					leftState = PuzzleUtil.EDGE_FLAT - utilList.get(curRow * coloum + (curColoum - 1)).getRightState();
				}
				if (curColoum == coloum - 1) {
					rightState = PuzzleUtil.EDGE_FLAT;
				}
				_util.setEdgeState(leftState, topState, rightState, bottomState);
				_util.setEdge(startX + perWidth * curColoum, startY + perHeight * curRow, 
						      startX + perWidth * (curColoum + 1), startY + perHeight * (curRow + 1));
				_util.setIndex(curRow * coloum + curColoum);
				utilList.add(_util);
			}
		}
	}
	
	public void clear() {
		utilList.clear();
		
		width = 0;
		height = 0;
		row = 1;
		coloum = 1;
		
		startX = 0;
		startY = 0;
	}
	
	public ArrayList<PuzzleUtil> getPuzzleUtilList() {
		return utilList;
	}
	
	public int size() {
		if (utilList == null) {
			return 0;
		}
		else {
			return utilList.size();
		}
	}
	
	public PuzzleUtil get(int index) {
		if (index < utilList.size()) {
			return utilList.get(index);
		}
		else {
			return null;
		}
	}
	
	public float getOffsetLeft(int index) {
		return (index % coloum) * (width / coloum);
	}
	
	public float getOffsetTop(int index) {
		return (index / coloum) * (height / row);
	}
	
	public float getPathWidth() {
		Log.v(TAG,"width " + width + " coloum " + coloum);
		return (width / coloum);
	}
	
	public float getPathHeight() {
		return (height / row);
	}
	
	public float getPathTop() {
		return startY;
	}
	
	public float getPathLeft() {
		return startX;
	}
	
	public float getTemplateWidth() {
		return width;
	}
	
	public float getTemplateHeight() {
		return height;
	}
}
